
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>在线考试系统</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            line-height: 1.6;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .login-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 30px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .question {
            background: white;
            margin-bottom: 20px;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }

        .question p {
            font-size: 16px;
            color: #333;
            margin-bottom: 15px;
        }

        .options label {
            display: block;
            margin: 10px 0;
            cursor: pointer;
        }

        .submit-btn {
            margin-top: 20px;
        }

        .error-message {
            color: #ff4444;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div id="app">
    <!-- 登录表单 -->
    <div v-if="!isLoggedIn" class="login-container">
        <h2>在线考试系统登录</h2>
        <form @submit.prevent="login">
            <div class="form-group">
                <label>用户名</label>
                <input
                        type="text"
                        v-model="loginForm.username"
                        required
                        placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label>密码</label>
                <input
                        type="password"
                        v-model="loginForm.password"
                        required
                        placeholder="请输入密码">
            </div>
            <button type="submit">登录</button>
            <div v-if="loginError" class="error-message">
                {{ loginError }}
            </div>
        </form>
    </div>

    <!-- 考试界面 -->
    <div v-else class="container">
        <h2>在线考试</h2>
        <div v-for="question in questions" :key="question.id" class="question">
            <p>{{ question.question }}</p>

            <!-- 单选题 -->
            <div v-if="question.type === 'single'" class="options">
                <label v-for="option in question.options" :key="option">
                    <input
                            type="radio"
                            :name="'q'+question.id"
                            :value="option"
                            v-model="answers[question.id]">
                    {{ option }}
                </label>
            </div>

            <!-- 判断题 -->
            <div v-if="question.type === 'judge'" class="options">
                <label>
                    <input type="radio" :name="'q'+question.id" value="true" v-model="answers[question.id]">
                    正确
                </label>
                <label>
                    <input type="radio" :name="'q'+question.id" value="false" v-model="answers[question.id]">
                    错误
                </label>
            </div>

            <!-- 填空题 -->
            <div v-if="question.type === 'fill'" class="form-group">
                <input
                        type="text"
                        v-model="answers[question.id]"
                        placeholder="请输入答案">
            </div>
        </div>

        <button @click="submitExam" class="submit-btn">提交试卷</button>
    </div>
</div>


<script>
    const { createApp } = Vue

    // 在script标签内添加
    const contextPath = window.location.pathname.substring(0, window.location.pathname.indexOf('/', 2));

    createApp({
        data() {
            return {
                isLoggedIn: false,
                loginForm: {
                    username: '',
                    password: ''
                },
                questions: [],
                answers: {},
                contextPath: contextPath // 添加上下文路径
            }
        },
        methods: {
            async login() {
                try {
                    const response = await axios.post(this.contextPath + '/api/login', new URLSearchParams(this.loginForm), {
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    });
                    if (response.data.success) {
                        this.isLoggedIn = true;
                        this.loadQuestions();
                    } else {
                        alert('登录失败：' + response.data.message);
                    }
                } catch (error) {
                    console.error('登录错误：', error);
                    alert('登录发生错误');
                }
            },
            async loadQuestions() {
                try {
                    const response = await axios.get(this.contextPath + '/api/exam');
                    this.questions = response.data;
                } catch (error) {
                    console.error('加载题目错误：', error);
                }
            },
            async submitExam() {
                try {
                    const response = await axios.post(this.contextPath + '/api/exam', this.answers);
                    if (response.data && response.data.score !== undefined) {
                        alert('您的得分是：' + response.data.score + '分');
                    } else {
                        alert('提交失败，请重试');
                    }
                } catch (error) {
                    console.error('提交试卷错误：', error);
                    alert('提交试卷时发生错误');
                }
            }
        }
    }).mount('#app')
</script>
</body>
</html>